package com.ruoyi.yyq.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.yyq.domain.*;
import com.ruoyi.yyq.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.utils.DateUtils;
/**
 * 服务核销记录Controller
 * 
 * @author 尹永青
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/record")
@CrossOrigin
public class CarVerificationRecordController extends BaseController
{
    @Autowired
    private ICarVerificationRecordService carVerificationRecordService;
    @Autowired
    private ICarMerchantBalanceService carMerchantBalanceService;
    @Autowired
    private ICarOrderService carOrderService;
    @Autowired
    private ICarServiceTypeService carServiceTypeService;

    /**
     * 查询服务核销记录列表
     */
    @RequiresPermissions("yyq:record:list")
    @GetMapping("/list")
    public TableDataInfo list(CarVerificationRecord carVerificationRecord)
    {
        startPage();
        carVerificationRecord.setDeleted(0L);
        List<CarVerificationRecord> list = carVerificationRecordService.selectCarVerificationRecordList(carVerificationRecord);
        return getDataTable(list);
    }

    /**
     * 根据商铺id查询服务核销记录列表
     */
//    @RequiresPermissions("yyq:record:list")
//    @PostMapping("/listByMid")
//    public AjaxResult listByMid(@RequestBody String mid){
////        CarVerificationRecord carVerificationRecord = new CarVerificationRecord();
////        carVerificationRecord.setMid(mid);
////        carVerificationRecord.setDeleted(0L);
////        List<CarVerificationRecord> list = carVerificationRecordService.selectCarVerificationRecordList(carVerificationRecord);
////        return success(list);
//        System.out.println(mid);
//        return success();
//    }

    @RequiresPermissions("yyq:record:list")
    @GetMapping("/listByMid/{mid}")
    public AjaxResult listByMid(@PathVariable("mid") Long mid){
        CarVerificationRecord carVerificationRecord = new CarVerificationRecord();
        carVerificationRecord.setMid(mid);
        carVerificationRecord.setDeleted(0L);
        List<CarVerificationRecord> list = carVerificationRecordService.selectCarVerificationRecordList(carVerificationRecord);
        list.forEach(item -> {
            Long typeId = carOrderService.selectCarOrderById(item.getOid()).getServiceTypeId();
            String type = carServiceTypeService.selectCarServiceTypeById(typeId).getType();
            item.setServiceTypeName(type);
        });
        return success(list);
    }

    /**
     * 导出服务核销记录列表
     */
    @RequiresPermissions("yyq:record:export")
    @Log(title = "服务核销记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarVerificationRecord carVerificationRecord)
    {
        List<CarVerificationRecord> list = carVerificationRecordService.selectCarVerificationRecordList(carVerificationRecord);
        ExcelUtil<CarVerificationRecord> util = new ExcelUtil<CarVerificationRecord>(CarVerificationRecord.class);
        util.exportExcel(response, list, "服务核销记录数据");
    }

    /**
     * 获取服务核销记录详细信息
     */
    @RequiresPermissions("yyq:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carVerificationRecordService.selectCarVerificationRecordById(id));
    }

    /**
     * 新增服务核销记录
     */
    @RequiresPermissions("yyq:record:add")
    @Log(title = "服务核销记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarVerificationRecord carVerificationRecord)
    {
        CarMerchantBalance carMerchantBalance = carMerchantBalanceService.selectCarMerchantBalanceByMid(carVerificationRecord.getMid());
        if (carMerchantBalance == null) {
            System.out.println("没有该商户");
            carMerchantBalance = new CarMerchantBalance();
            carMerchantBalance.setMid(carVerificationRecord.getMid());
            carMerchantBalance.setInitialBalance(BigDecimal.valueOf(carVerificationRecord.getPrice()));
            carMerchantBalanceService.insertCarMerchantBalance(carMerchantBalance);
        }else {
            System.out.println("有该商户");
            Long priceLong = carVerificationRecord.getPrice();
            // 将 Long 类型转换为 BigDecimal 类型
            BigDecimal price = BigDecimal.valueOf(priceLong);
            // 使用 BigDecimal 的 add 方法进行加法运算
            price = price.add(carMerchantBalance.getInitialBalance());
            carMerchantBalance.setInitialBalance(price);
            carMerchantBalanceService.updateCarMerchantBalance(carMerchantBalance);
        }
        carVerificationRecord.setTime(DateUtils.getNowDate());
        return toAjax(carVerificationRecordService.insertCarVerificationRecord(carVerificationRecord));
    }

    /**
     * 修改服务核销记录
     */
    @RequiresPermissions("yyq:record:edit")
    @Log(title = "服务核销记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarVerificationRecord carVerificationRecord)
    {
        return toAjax(carVerificationRecordService.updateCarVerificationRecord(carVerificationRecord));
    }

    /**
     * 删除服务核销记录
     */
    @RequiresPermissions("yyq:record:remove")
    @Log(title = "服务核销记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carVerificationRecordService.deleteCarVerificationRecordByIds(ids));
    }
}
